import { createApp } from 'vue';
import MkDialogConfirm from './MkDialogConfirm.vue';

/**
 * 延时删除确认对话框
 * @param props 弹框的配置项
 * @param searchFun 弹框调用的检索接口
 * @returns
 */
export function useConfirmDialog(message: string, title: string = '确认') {
	let instance: any;

	if (!instance) {
		// 第二个参数以组件的props传入
		const app = createApp(MkDialogConfirm, { message, title });
		const container = document.createElement('div');
		instance = app.mount(container);
		document.body.appendChild(container);
	}

	return new Promise((resolve, reject) => {
		// 确认框回调
		const confirmCallback = (show: boolean) => {
			resolve(show);
		};
		// 取消/关闭
		const cancelCallback = () => {
			reject(undefined);
		};
		// 弹框消失前销毁组件
		const destory = () => {
			if (instance) {
				instance.$el.parentNode.remove();
				instance = null;
			}
		};
		instance.config = { confirmCallback, cancelCallback, destory };
		instance.openDialog();
	});
}
